<?php
// +----------------------------------------------------------------------
// | 随手拍活动主题
// +----------------------------------------------------------------------

namespace Toppic\Controller;
use Common\Controller\Base;
use Common\Model\Model;

class SuipaiController extends Base {

    //初始化可用参数
    protected function _initialize(){
        parent::_initialize();
        $this->assign('nav', $this->getNav());
    }

    /**
     * 获取导航
     * @return array
     */
    public function getNav(){
        $nav=array(
            'index'=>array(
                'link'=>'index',
                'title'=>'首页',
            ),
            'rule'=>array(
                'link'=>'rule',
                'title'=>'活动规则',
            ),
            'show'=>array(
                'link'=>'show',
                'title'=>'作品展示',
            ),
            'winner'=>array(
                'link'=>'winner',
                'title'=>'获奖公布',
            )
        );
        $curr=strtolower(ACTION_NAME);
        foreach($nav as $key => $value){
            if($curr==$key){
                $nav[$key]['act']=' act';
            }
        }
        return $nav;
    }

    //首页
    public function index(){

        $qflist=M('ToppicSuipai')->field('userid,workid,count(id) as counts')->where(array('status'=>true))->group('userid')->order('counts DESC, id DESC')->limit(60)->select();

        foreach($qflist as $key=>$value){
            $qflist[$key]['suipai']=M('Suipai')->field('title,image,clicknum,msgnum')->where(array('id'=>$value['workid'],'status'=>true))->find();
            $qflist[$key]['user']=M('User')->field('nickname,openuid,uavatar')->where(array('userid'=>$value['userid']))->find();
        }
        $this->assign('qflist',$qflist);

        $Model=new Model();
        $hdlist=$Model
            ->table(array(
                    C('DB_PREFIX').'toppic_suipai'=>'TS',
                    C('DB_PREFIX').'suipai'=>'S',
                    C('DB_PREFIX').'user'=>'U',
                )
            )
            ->where('TS.userid=U.userid and S.status=true and S.id=TS.workid')
            ->field('S.title,S.id,TS.create_time,S.image,U.uavatar,U.openuid')
            ->order('S.msgnum DESC')
            ->limit(40)
            ->select();
        $this->assign('hdlist',$hdlist);

        $this->display();
    }

    //活动规则
    public function rule(){
        $this->display();
    }

    //作品展示
    public function show(){
        $sort=I('get.sort');
        $type=I('get.type');

        if(empty($type)){
            //如果不是勤奋达人
            if(empty($sort)){
                $order='TS.id';
            }else{
                if($sort=='rq'){
                    $order=' S.clicknum';
                }elseif($sort=='msg'){
                    $order=' S.msgnum';
                }
            }
            $this->assign('sort',$sort);

            $Model=new Model();
            $count=$Model
                ->table(array(
                        C('DB_PREFIX').'toppic_suipai'=>'TS',
                        C('DB_PREFIX').'suipai'=>'S',
                    )
                )
                ->where('S.status=true and S.id=TS.workid')
                ->count();
            $page = $this->Pages($count, 15);
            $pageurl='&page='.urlencode('[PAGE]')."#list";
            $page->url     = __APP__.'/toppic/suipai/show?sort='.$sort.$pageurl;

            $list=$Model
                ->table(array(
                        C('DB_PREFIX').'toppic_suipai'=>'TS',
                        C('DB_PREFIX').'suipai'=>'S',
                        C('DB_PREFIX').'user'=>'U',
                    )
                )
                ->where('TS.userid=U.userid and S.status=true and S.id=TS.workid')
                ->field('S.title,S.msgnum,S.clicknum,S.id,TS.create_time,S.image,U.uavatar,U.openuid,TS.userid')
                ->order($order.' DESC')
                ->limit($page->firstRow . ',' . $page->listRows)
                ->select();

        }else{
            //如果是勤奋达人
            $this->assign('type',$type);

            $Model=new Model();

            $arr=$Model
                ->table(array(
                        C('DB_PREFIX').'toppic_suipai'=>'TS',
                        C('DB_PREFIX').'suipai'=>'S',
                        C('DB_PREFIX').'user'=>'U',
                    )
                )
                ->where('TS.userid=U.userid and S.status=true and S.id=TS.workid')
                ->field('TS.id')
                ->group('TS.userid')
                ->select();
            $count=count($arr);

            $page = $this->Pages($count, 15);
            $pageurl='&page='.urlencode('[PAGE]')."#list";
            $page->url     = __APP__.'/toppic/suipai/show?type=daren'.$pageurl;


            $list=M('ToppicSuipai')->field('userid,workid,count(id) as counts')->where(array('status'=>true))->group('userid')->order('counts DESC, id DESC')->limit($page->firstRow . ',' . $page->listRows)->select();

            foreach($list as $key=>$value){
                $list[$key]['suipai']=M('Suipai')->field('title,image,clicknum,msgnum')->where(array('id'=>$value['workid'],'status'=>true))->find();
                $list[$key]['user']=M('User')->field('nickname,openuid,uavatar')->where(array('userid'=>$value['userid']))->find();
            }
        }
        foreach($list as $key=>$value){
            $list[$key]['worknum']=M('ToppicSuipai')->where(array('userid'=>$value['userid']))->count();
        }


        $this->pagecount=$page->totalRows/$page->listRows;

        $this->assign("list", $list);
        $this->assign("Page", $page->show());

        $this->display();
    }


    //奖品公布
    public function winner(){

        $qflist=M('ToppicSuipai')->field('userid,workid,count(id) as counts')->where(array('status'=>true))->group('userid')->order('counts DESC, id DESC')->limit(60)->select();

        foreach($qflist as $key=>$value){
            $qflist[$key]['suipai']=M('Suipai')->field('title,image,clicknum,msgnum')->where(array('id'=>$value['workid'],'status'=>true))->find();
            $qflist[$key]['user']=M('User')->field('nickname,openuid,uavatar')->where(array('userid'=>$value['userid']))->find();
        }
        $this->assign('qflist',$qflist);

        $Model=new Model();
        $hdlist=$Model
            ->table(array(
                    C('DB_PREFIX').'toppic_suipai'=>'TS',
                    C('DB_PREFIX').'suipai'=>'S',
                    C('DB_PREFIX').'user'=>'U',
                )
            )
            ->where('TS.userid=U.userid and S.status=true and S.id=TS.workid')
            ->field('S.title,S.id,TS.create_time,S.image,U.uavatar,U.openuid,U.nickname')
            ->order('S.msgnum DESC')
            ->limit(40)
            ->select();
        $this->assign('hdlist',$hdlist);

        $this->display();
    }



}